# -*- coding: utf-8 -*-

"""
 (c) 2023 - Copyright CTyunOS Inc

 Authors:
   youyifeng <youyf2@chinatelecom.cn>

"""

import json
import logging
from optparse import OptionParser

from cve_ease.helper import get_watcher

logger = logging.getLogger('cve-ease')


def get_usage_str(usage):
    return usage + "\n(Specify the --help global option for a list of other help options)"


def handle_watcher(gconfig, db_session, args):
    """[basic] Watcher tools"""
    usage = "usage: %prog watcher <options>"
    parser = OptionParser(usage=get_usage_str(usage))

    parser.add_option('-j', '--json', dest='json', action='store_true', default=False,
                      help='show watcher config with json')
    parser.add_option('-l', '--list', dest='list', action='store_true', default=False,
                      help='list watcher config')

    (options, args) = parser.parse_args(args)

    if options.json:
        watcherinfo = get_watcher(gconfig)
        print(json.dumps(watcherinfo, indent=4))
    elif options.list:
        watcherinfo = get_watcher(gconfig)
        for w in watcherinfo:
            cw = watcherinfo[w]
            print(f"|-- section {w}")
            for key in cw:
                print("|    |-- %s : %s" % (key, cw[key]))
        print("|________")
    else:
        parser.print_help()
